<%-- 
    Document   : productList
    Created on : May 9, 2013, 2:38:42 PM
    Author     : Kate
--%>
<%@page import="utils.ConfigurationManager"%>
<%@page import="java.io.File"%>
<%@page import="javax.naming.NamingException"%>
<%@page import="javax.naming.InitialContext"%>
<%@page import="facade.CategoryFacadeLocal"%>
<%@page import="facade.ProductFacadeLocal"%>
<%@page import="entity.Category"%>
<%@page import="entity.Product"%>
<%@page import="java.util.List"%>
<script type="text/javascript" src="scripts/jquery-1.8.3.min.js"></script>

<%! private ProductFacadeLocal productFacade;
    private CategoryFacadeLocal categoryFacade;

    public void jspInit() {
        try {
            InitialContext ic = new InitialContext();
            productFacade = (ProductFacadeLocal) ic.lookup(
                    "java:global/Delta/Delta-ejb/ProductFacade!facade.ProductFacadeLocal");
            categoryFacade = (CategoryFacadeLocal) ic.lookup(
                    "java:global/Delta/Delta-ejb/CategoryFacade!facade.CategoryFacadeLocal");


        } catch (NamingException ex) {
            ex.printStackTrace();
        }
    }
%>

<%
    int pageNumber;
    String pageNum;
    int maxResults;
    int firstResult;
    String id_category_p;
    int id_category;
    List<Product> products = null;
    String productName;
    String sortBy;
    int sortField;
    boolean isAsc;
    Category category;
    long count = -1;
%>
<%

    //parse pageNumber
    pageNum = request.getParameter("pageNumber");
    try {
        pageNumber = Integer.parseInt(pageNum);
    } catch (NumberFormatException ex) {
        pageNumber = 1;
    }
    maxResults = 10;
    firstResult = maxResults * (pageNumber - 1);

    //parse category
    id_category_p = request.getParameter("category");
    try {
        id_category = Integer.parseInt(id_category_p);
    } catch (NumberFormatException ex) {
        id_category = -1;
    }

    //parse name
    productName = request.getParameter("name");
    if (productName == null) {
        productName = "";
    }

    //parse sortBy
    String sortBy_p = request.getParameter("sortBy");
    try {
        sortField = Integer.parseInt(sortBy_p);
    } catch (NumberFormatException ex) {
        sortField = 0;
    }

    switch (sortField) {
        case 1:
            sortBy = "price";
            isAsc = true;
            break;
        case 2:
            sortBy = "price";
            isAsc = false;
            break;
        case 3:
            sortBy = "name";
            isAsc = false;
            break;
        default:
            sortBy = "name";
            isAsc = true;
            break;
    }

%>

<%
    if (productName != "" && id_category != -1) {
        category = categoryFacade.find(id_category);
        products = productFacade.findActive(firstResult, maxResults, sortBy, isAsc, productName, category);
        count = productFacade.countActiveByNameCategory(productName, categoryFacade.find(id_category));
    }
    if (productName != "" && id_category == -1) {
        products = productFacade.findActive(firstResult, maxResults, sortBy, isAsc, productName);
        count = productFacade.countActiveByName(productName);
    }
    if (productName == "" && id_category != -1) {
        category = categoryFacade.find(id_category);
        products = productFacade.findActive(firstResult, maxResults, sortBy, isAsc, category);
        count = productFacade.countActiveByCategory(category);

    }
    if (productName == "" && id_category == -1) {
        products = productFacade.findActive(firstResult, maxResults, sortBy, isAsc);
        count = productFacade.countActive();
    }

    if (products.isEmpty()) {
        out.print("<center><font color=\"red\"><b>Sorry, no products found on your search<b></font></center>");
    }
%>
<script>
    $(document).ready(function() {
        $("img").error(function() {
            alert('BRRRRRRRRRRRRRRRRR');
            $(this).unbind("error").attr("src", "broken.gif");
        });
    });</script><%

 for (Product prod : products) {
    %>

<div id="product">

    <div id="picture">
        <% String filePathString = ConfigurationManager.getInstance().getLinksProperties().getProperty("PICTURES_FILE_PATH");
        if(prod.getPicture()!=null){
        filePathString +=  prod.getPicture().substring(8);
        }
            if (new File(filePathString).isFile()) {%>
        <img src=<%=prod.getPicture()%>  /> 
        <%} else {

        %><img src="/Delta-war/product/NoImage.jpg"  /> 
        
        <%}%>
    </div>
    <div id="describe">
        <h3><%=prod.getName()%> </h3>
        <p> <%=prod.getDescription()%> </p>   
    </div>
    <div id="bought" >
        <h4>Price:  <%=prod.getPrice()%> $</h4>  

        <form 
            method="Post" 
            action="AddToCart">
            <p> 
                Quantity: 
                <input 
                    id="quantity_<%=prod.getId_product()%>"
                    name="quantity" 
                    type="number" 
                    min="1" 
                    value="1" 
                    style="width:40 "  />
            </p>

            <p> 
                <input
                    name="id_product"
                    value="<%=prod.getId_product()%>"  
                    type="hidden" />
                <input id="buying"
                       type="button"
                       onclick="if (validateFields())
            addProduct(<%=prod.getId_product()%>)"
                       name="buy"
                       value="Buy"
                       /> 
            </p>
        </form>
    </div>
    <div style="clear: left">
    </div>
</div>

<% }%>

<input
    id="count"
    name="count"
    type="hidden"
    value="<%=count%>"/>


